//
//  UIView+Tool.m
//  BaseProject
//
//  Created by 小崔 on 2022/4/21.
//

#import "UIView+Tool.h"

@implementation UIView (Tool)

#pragma mark --切圆角相关
/// 继承于UIView的视图切圆角
/// @param cornerType 圆角类型， 可看系统自带类型
/// @param radius 圆角角度
- (void)crj_setCornerType:(UIRectCorner)cornerType
             cornerRadius:(CGFloat)radius {
    CGSize size = CGSizeMake(radius, radius);
    [self sign_cornerType:cornerType cornerSizeRadius:size];
}
/// 继承于UIView的视图切圆角
/// @param rect 当前控件frame
/// @param cornerType 剪切类型
/// @param radius 圆角的角度
- (void)crj_setRoundedRect:(CGRect)rect
                cornerType:(UIRectCorner)cornerType
          cornerSizeRadius:(CGFloat)radius {
    CGSize reSize = CGSizeMake(radius, radius);
    self.crj_bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect
                                               byRoundingCorners:cornerType
                                                     cornerRadii:reSize];
    self.crj_shapeLayer = [self get_shapeLayer];
    self.crj_shapeLayer.path = self.crj_bezierPath.CGPath;
    self.layer.mask = self.crj_shapeLayer;
}

- (void)sign_cornerType:(UIRectCorner)cornerType
       cornerSizeRadius:(CGSize)size{
    NSLog(@"width :%f   height : %f",self.frame.size.width,self.frame.size.height);
    self.crj_bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) byRoundingCorners:cornerType cornerRadii:CGSizeMake(size.width, size.height)];
    self.crj_shapeLayer = [self get_shapeLayer];
    self.crj_shapeLayer.path = self.crj_bezierPath.CGPath;
    self.layer.mask = self.crj_shapeLayer;
}
- (CAShapeLayer *)get_shapeLayer{
    CAShapeLayer * layers = [CAShapeLayer layer];
    layers.frame = self.bounds;
    return layers;
}
#pragma mark - Associated Object
- (void)setCrj_shapeLayer:(id)object{
    objc_setAssociatedObject(self, @selector(crj_shapeLayer), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CAShapeLayer *)crj_shapeLayer{
    return objc_getAssociatedObject(self, @selector(crj_shapeLayer));
}

- (void)setCrj_bezierPath:(id)object{
    objc_setAssociatedObject(self, @selector(crj_bezierPath), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIBezierPath *)crj_bezierPath{
    return objc_getAssociatedObject(self, @selector(crj_bezierPath));
}

#pragma mark --添加阴影
/**
 给UIView添加阴影
 
 @param shadowColor 阴影颜色
 @param shadowOpacity 阴影透明度 默认0
 @param shadowRadius 阴影半径 也就是阴影放射程度 默认3
 @param shadowType 阴影方向
 @param shadowWidth 阴影放射宽度
 */
- (void)viewShadowWithColor:(UIColor *)shadowColor
              shadowOpacity:(CGFloat)shadowOpacity
               shadowRadius:(CGFloat)shadowRadius
             shadowPathType:(CRJShadowType)shadowType
                shadowWidth:(CGFloat)shadowWidth {
    self.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
    self.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
    self.layer.shadowOpacity = shadowOpacity;// 阴影透明度，默认0
    self.layer.shadowOffset = CGSizeZero;//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    self.layer.shadowRadius = shadowRadius;//阴影半径，默认3
    CGRect shadowRect = CGRectZero;
    CGFloat originX,originY,sizeWidth,sizeHeight;
    originX = 0;
    originY = 0;
    sizeWidth = self.bounds.size.width;
    sizeHeight = self.bounds.size.height;
    
    if (shadowType == CRJShadowTop) {
        shadowRect = CGRectMake(originX, originY-shadowWidth, sizeWidth, shadowWidth*2);
    }else if (shadowType == CRJShadowBottom){
        shadowRect = CGRectMake(originX, sizeHeight-shadowWidth, sizeWidth, shadowWidth*2);
    }else if (shadowType == CRJShadowLeft){
        shadowRect = CGRectMake(originX-shadowWidth, originY, shadowWidth*2, sizeHeight);
    }else if (shadowType == CRJShadowRight){
        shadowRect = CGRectMake(sizeWidth-shadowWidth, originY, shadowWidth*2, sizeHeight);
    }else if (shadowType == CRJShadowTopBottom){
        shadowRect = CGRectMake(originX+(shadowWidth+2), originY-shadowWidth, sizeWidth-(shadowWidth+2)*2, sizeHeight+shadowWidth*2);
    }else if (shadowType == CRJShadowLeftRight){
        shadowRect = CGRectMake(originX-shadowWidth, originY+(shadowWidth+2), sizeWidth+shadowWidth*2, sizeHeight-(shadowWidth+2)*2);
    }else if (shadowType == CRJShadowTopLeftRight){
        shadowRect = CGRectMake(originX-shadowWidth, originY-shadowWidth, sizeWidth+shadowWidth*2, sizeHeight-2);
    }else if (shadowType == CRJShadowBottomLeftRight){
        shadowRect = CGRectMake(originX-shadowWidth, originY+shadowWidth+2, sizeWidth+shadowWidth*2, sizeHeight);
    }else if (shadowType == CRJShadowAround){
        shadowRect = CGRectMake(originX-shadowWidth, originY-shadowWidth, sizeWidth+shadowWidth*2, sizeHeight+shadowWidth*2);
    }
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
    self.layer.shadowPath = bezierPath.CGPath;//阴影路径
}
/**
 给不规则UIView添加阴影
 
 @param shadowColor 阴影颜色
 @param shadowOpacity 阴影透明度 默认0
 @param shadowRadius 阴影半径 也就是阴影放射程度 默认3
 @param shadowType 阴影方向
 @param shadowWidth 阴影放射宽度
 */
- (void)viewShadowWithBackgroundViewColor:(UIColor *)shadowColor
                            shadowOpacity:(CGFloat)shadowOpacity
                             shadowRadius:(CGFloat)shadowRadius
                           shadowPathType:(CRJShadowType)shadowType
                              shadowWidth:(CGFloat)shadowWidth {
    self.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
    self.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
    self.layer.shadowOpacity = shadowOpacity;// 阴影透明度，默认0
    self.layer.shadowOffset = CGSizeZero;//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    CGFloat originX,originY,sizeWidth,sizeHeight;
    originX = 0;
    originY = 0;
    sizeWidth = self.bounds.size.width;
    sizeHeight = self.bounds.size.height;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    if (shadowType == CRJShadowTop) {
        [bezierPath moveToPoint:CGPointMake(-shadowWidth, sizeHeight)];
        [bezierPath addLineToPoint:CGPointMake(-shadowWidth, -shadowWidth+shadowRadius)];//1
        //添加外部线左圆角
        [bezierPath addArcWithCenter:CGPointMake(-shadowWidth+shadowRadius, -shadowWidth+shadowRadius) radius:shadowRadius startAngle:M_PI endAngle:M_PI/2*3 clockwise:YES];
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth-shadowRadius, -shadowWidth)];//2
        //添加外部线右圆角
        [bezierPath addArcWithCenter:CGPointMake(sizeWidth+shadowWidth-shadowRadius, -shadowWidth+shadowRadius) radius:shadowRadius startAngle:M_PI/2*3 endAngle:0 clockwise:YES];
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth, sizeHeight)];//3
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, sizeHeight)];//4
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, shadowRadius)];//5
        //添加内部线右圆角
        [bezierPath addArcWithCenter:CGPointMake(sizeWidth-shadowRadius, shadowRadius) radius:shadowRadius startAngle:0 endAngle:M_PI/2*3 clockwise:NO];
        [bezierPath addLineToPoint:CGPointMake(shadowRadius, 0)];//6
        //添加内部线左圆角
        [bezierPath addArcWithCenter:CGPointMake(shadowRadius, shadowRadius) radius:shadowRadius startAngle:M_PI/2*3 endAngle:M_PI clockwise:NO];
        [bezierPath addLineToPoint:CGPointMake(0, sizeHeight)];//7
    }else if (shadowType == CRJShadowBottom){
        [bezierPath moveToPoint:CGPointMake(-shadowWidth, 0)];
        [bezierPath addLineToPoint:CGPointMake(-shadowWidth, sizeHeight+shadowWidth-shadowRadius)];//1
        //添加外部线左圆角
        [bezierPath addArcWithCenter:CGPointMake(-shadowWidth+shadowRadius, sizeHeight+shadowWidth-shadowRadius) radius:shadowRadius startAngle:M_PI endAngle:M_PI_2 clockwise:NO];
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth-shadowRadius, sizeHeight+shadowWidth)];//2
        //添加外部线右圆角
        [bezierPath addArcWithCenter:CGPointMake(sizeWidth+shadowWidth-shadowRadius, sizeHeight+shadowWidth-shadowRadius) radius:shadowRadius startAngle:M_PI_2 endAngle:0 clockwise:NO];
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth, 0)];//3
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, 0)];//4
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, sizeHeight-shadowRadius)];//5
        //添加内部线右圆角
        [bezierPath addArcWithCenter:CGPointMake(sizeWidth-shadowRadius, sizeHeight-shadowRadius) radius:shadowRadius startAngle:0 endAngle:M_PI_2 clockwise:YES];
        [bezierPath addLineToPoint:CGPointMake(shadowRadius, sizeHeight)];//6
        //添加内部线左圆角
        [bezierPath addArcWithCenter:CGPointMake(shadowRadius, sizeHeight-shadowRadius) radius:shadowRadius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
        [bezierPath addLineToPoint:CGPointMake(0, 0)];//7
    }else if (shadowType == CRJShadowMiddle){
        CGFloat middleLineHeight = (sizeHeight-shadowWidth)*0.5;
        [bezierPath moveToPoint:CGPointMake(-shadowWidth, sizeHeight)];
        [bezierPath addLineToPoint:CGPointMake(-shadowWidth, 0)];//1
        [bezierPath addLineToPoint:CGPointMake(0, 0)];//2
        [bezierPath addLineToPoint:CGPointMake(0, middleLineHeight)];//3
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, middleLineHeight)];//4
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, 0)];//5
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth, 0)];//6
        [bezierPath addLineToPoint:CGPointMake(sizeWidth+shadowWidth, sizeHeight)];//7
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, sizeHeight)];//8
        [bezierPath addLineToPoint:CGPointMake(sizeWidth, sizeHeight-middleLineHeight)];//9
        [bezierPath addLineToPoint:CGPointMake(0, sizeHeight-middleLineHeight)];//10
        [bezierPath addLineToPoint:CGPointMake(0, sizeHeight)];//11
    }else if (shadowType == CRJShadowAround){
        self.layer.shadowRadius = shadowRadius;
        bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, sizeWidth, sizeHeight)];
    }
    [bezierPath closePath];
    self.layer.shadowPath = bezierPath.CGPath;//阴影路径
}
// 添加四边阴影效果
- (void)addShadowToViewColor:(UIColor *)theColor
               shadowOpacity:(CGFloat)shadowOpacity
                shadowRadius:(CGFloat)shadowRadius {
    self.layer.masksToBounds = NO;
    // 阴影颜色
    self.layer.shadowColor = theColor.CGColor;
    // 阴影偏移，默认(0, -3)
    self.layer.shadowOffset = CGSizeMake(0,0);
    // 阴影透明度，默认0
    self.layer.shadowOpacity = shadowOpacity;
    // 阴影半径，默认3
    self.layer.shadowRadius = shadowRadius;
}

- (UIImage *)generateImage {
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

@end
